// JavaScript Document
$(function(){
	var word_number=128;
	var tips='';
	var $comment=$("#comment_content");
	var $count=$("#word_count");
	$("#word_count").text(word_number);
	$comment.attr({"value":tips,"maxlength":word_number});
	$comment.focusin(function(){
		if($(this).val()==tips){
			$(this).attr("value",'');
		}
	}).focusout(function(){
		if($(this).val()==''){
			$(this).attr("value",tips);
		}
	}).keyup(function(){
		var length=$(this).val().length;
		$count.text(word_number-length);
	}).keydown(function(){
		if($(this).val().length >= word_number){
			showMsg('您的输入已经超过了'+word_number+'个字符！',0);
		}
		if($(this).val().length > word_number){
			return false;
		}
	})
	//保存
	$("#comment_button").click(function(){
		var artId=$(this).attr("articleId");
		var name=$("#comment_name").val();
		var email=$("#comment_email").val();
		var content=$comment.val();
		var patrn=/^[a-zA-Z\u4E00-\u9FA5]+$/;
		if(name==''){
			showMsg('请输入您的昵称！',0);
			$("#comment_name").focus();
			return false;
		}else if(!patrn.test(name)){
			showMsg('昵称只能由字母和中文组成！',0);
			$("#comment_name").focus();
			return false;	
		}
		if(email==''){
			showMsg('请输入您的邮箱地址！',0);
			$("#comment_email").focus();
			return false;
		}else if(!is_email(email)){
			showMsg('对不起，不是正确的邮箱号码！',0);
			$("#comment_email").focus();
			return false;
		}
		if(content==''||content==tips){
			showMsg('请输入评论内容！',0);
			$comment.focus();
			return false;
		}else if($comment.val().length > word_number){
			showMsg('评论内容已经超过了'+word_number+'个字符！',0);
			$comment.focus();
			return false;
		}else{
			/*$.each(emotions,function(k,v){
				v=v.split("#");
				var img='<img src="'+v[1]+'" title="'+v[0].replace('/','')+'" />';
				var reg=new RegExp("\\"+v[0],"g");
				content=content.replace(reg,img);
			})*/
			$.ajax({
				data:"id="+artId+"&name="+name+"&email="+email+"&content="+encodeURIComponent(content)+"&ajax=1&rd="+Math.random(),
				type:"get",
				dataType:"json",
				url:URL+'/createComment',
				success:function(data){
					if(data.status==1){
						showMsg(data.info,1);
						$comment.attr("value",tips);
						var number=parseInt($("#comment_count").text());
						if(number < 1){
							$("#no_comment").remove();
						}
						$count.text(word_number);
						$("#comment_count").text(number+1);
						var html='<table cellpadding="0" cellspacing="0"><tr><th width="100"><a></a></th><td><div class="comment_content"><div class="clr tl">'+data.data.content+'</div><div class="clr tr"><span class="comment_time">'+data.data.add_time+'</span> | <span class="comment_name">'+data.data.name+'</span></div></td></tr></table>';
						$("#comment_list").append(html);
						return false;
					}else{
						showMsg(data.info,0);
						return false;
					}
				}
			})
		}
	})
})